using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class CallNumberItem : BaseItem
	{
		private const int MIN_NUM = 1;

		private const int MAX_NUM = 10;

		private int num;

		private SpriteRenderer[] nums;

		public override void Init(ItemArchiveData data)
		{
            base.Init(data);
			nums = new SpriteRenderer[2];
			nums[0] = model.Find("0").GetComponent<SpriteRenderer>();
			nums[1] = model.Find("1").GetComponent<SpriteRenderer>();
			InitNumber(data.state);
		}

		public void InitNumber(int val)
		{
			if (val < 1 || val > 10)
			{
				val = 1;
			}
			num = val;
			string[] array = new string[2];
			if (val < 10)
			{
				array[0] = "0";
				array[1] = val.ToString();
			}
			else
			{
				array[0] = num.ToString().Substring(0, 1);
				array[1] = num.ToString().Substring(1, 1);
			}
			SpriteAssets component = model.GetComponent<SpriteAssets>();
			for (int i = 0; i < array.Length; i++)
			{
				string value = array[i];
				if (!(component != null) || !(component.data != null) || component.data.sprites == null)
				{
					continue;
				}
				for (int j = 0; j < component.data.sprites.Count; j++)
				{
					if (component.data.sprites[j] != null && component.data.sprites[j].name.Equals(value))
					{
						nums[i].sprite = component.data.sprites[j];
						break;
					}
				}
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			InitNumber(num + 1);
			Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = num;
		}
	}
}
